<?php

class Rrd_Database {
	private $name = "N/A";
	private $file;
	private $data;

	private static $path = "./db/";

	public static function setPath($path) {
		Rrd_Database::$path = $path;
	}

	public function __construct($name, $file) {
		$this->name = $name;
		$this->file = Rrd_Database::$path . "/" . $file . ".rrd";
		Log_Logger::get()->debug("RRD database file: " . $this->file);
		if(!file_exists($this->file)) {
			Log_Logger::get()->info("Database " . basename($this->file) . " don't exist, creating it");
			$this->create();
		}
	}

	public function update($value) {
		$cmd = sprintf("rrdtool update \"%s\" N:%s", $this->file, $value);
		Log_Logger::get()->debug("Command: %s", $cmd);
		Util_Shell::exec($cmd, array(Log_Logger::get(), "debug"));
	}

	private function readData() {
		if(!is_array($this->data)) {
			$cmd = sprintf("/usr/bin/rrdtool fetch \"%s\" AVERAGE -r 60 -s -1d -e -300", $this->file);
			Log_Logger::get()->debug("Command: %s", $cmd);
			exec($cmd, $output, $return);
			$this->data = array();
			foreach($output as $line) {
				$tmp = explode(": ", $line, 2);
				if(count($tmp) == 2) {
				list($k, $v) = $tmp;
					$v = str_replace(",", ".", $v);
					if(is_numeric($v)) $v += 0;
					$this->data[$k] = $v;
				}
			}
		}
		return $this->data;
	}

	public function getMax($round = 2) {
		$data = $this->readData();
		$max = null;
		foreach($data as $value) {
			if(is_numeric($value) ) {
				if($max == null || $max < $value) {
					$max = $value;
				}
			}
		}
		return is_numeric($max) ? round($max, $round) : $max;
	}
	
	public function getMin($round = 2) {
		$data = $this->readData();
		$min = null;
		foreach($data as $value) {
			if(is_numeric($value) ) {
				if($min == null || $min > $value) {
					$min = $value;
				}
			}
		}
		return is_numeric($min) ? round($min, $round) : $min;
	}

	public function getAvg($round = 2) {
		$data = $this->readData();
		$sum = 0;
		$values = 0;
		foreach($data as $value) {
			if(is_numeric($value) ) {
				$sum += $value;
				$values++;
			}
		}
		return round($sum/$values, $round);
	}
	
	public function getCurrent($round = 2) {
		$value = array_pop($this->readData());
		return is_numeric($value) ? round($value, $round) : $value;
	}

	public function getGraphCommand($color = "#0000FF") {
		$name = $this->name;
		$cmd = "DEF:$name=" . $this->file . ":temp:AVERAGE ";
		$cmd .= "LINE1:$name$color:\"" . sprintf("%-15s", $name) . "\" ";
		$cmd .= "GPRINT:$name:MAX:\"       Max\\: %4.1lf\" ";
		$cmd .= "GPRINT:$name:MIN:\"       Min\\: %4.1lf\" ";
		$cmd .= "GPRINT:$name:AVERAGE:\"       Avg\\: %4.1lf\" ";
		$cmd .= "COMMENT:\"\\n\"";
		return $cmd;
	}

	private function create() {
		$cmd  = "/usr/bin/rrdtool create \"" . $this->file . "\" --start N --step 300 ";
		$cmd .= "DS:temp:GAUGE:600:U:U ";
		$cmd .= "RRA:AVERAGE:0.5:1:2016 ";
		$cmd .= "RRA:MIN:0.5:1:2016 ";
		$cmd .= "RRA:MAX:0.5:1:2016 ";
		$cmd .= "RRA:AVERAGE:0.5:6:1344 ";
		$cmd .= "RRA:MIN:0.5:6:1344 ";
		$cmd .= "RRA:MAX:0.5:6:1344 ";
		$cmd .= "RRA:AVERAGE:0.5:24:2190 ";
		$cmd .= "RRA:MIN:0.5:24:2190 ";
		$cmd .= "RRA:MAX:0.5:24:2190 ";
		$cmd .= "RRA:AVERAGE:0.5:144:3650 ";
		$cmd .= "RRA:MIN:0.5:144:3650 ";
		$cmd .= "RRA:MAX:0.5:144:3650 ";
		Util_Shell::exec($cmd, array(Log_Logger::get(), "debug"));
	}
}

?>
